<template>
  <div class="ssk">
    <el-container>
      <el-input
        style="width: 20%"
        placeholder="运动编码/名称"
        v-model="parameter.content"
        clearable
      ></el-input>
      <el-button
        type="primary"
        size="mini"
        @click="onSubmit"
        icon="el-icon-search"
        round
        >查询</el-button
      >
    </el-container>
    <el-container>
      <el-button
        type="success"
        size="mini"
        icon="el-icon-circle-plus"
        @click.native="addTSportsItem()"
        round
        >添加</el-button
      >
    </el-container>
    <el-table
      style="width: 100%"
      align="center"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      :data="AllUser"
      @selection-change="handleSelectionChange">
      <el-table-column label="编码" prop="code" width="120"> </el-table-column>
      <el-table-column label="项目名称" prop="name" width="120">
      </el-table-column>
      <el-table-column label="运动强度" prop="strength" width="120">
      </el-table-column>
      <el-table-column label="千步当量" prop="thousandStepEquivalent" width="120">
      </el-table-column>
      <el-table-column label="运动时间(分钟)" prop="movementTime" width="120">
      </el-table-column>
      <el-table-column label="年龄区间" prop="count" width="300">
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click.native="update(scope.$index, scope.row)"
            icon="el-icon-edit"
          >
            修改
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click.native="deleteRow(scope.$index, scope.row)"
            icon="el-icon-delete"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="parameter.page"
      :page-sizes="[2, 5, 10, 20]"
      :pager-count="9"
      background
      :page-size.sync="parameter.num"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <el-dialog :visible.sync="dialogFormVisible" width="1000px">
                        <el-form :model="form" label-width="80px">
                            <el-row>
                                <el-col :span="10" :offset="1">
                                  <dh style="color:red">*</dh>项目名称:<el-input v-model="form.name" size="mini" @blur="byid()" :disabled="bj2"/>
                                </el-col>
                                <el-col :span="10" :offset="1">
                            <dh style="color:red">*</dh>适用性别:<el-select v-model="form.applyToGender" size="mini" style="width: 160px;">
                                        <el-option label="不限" value="2"></el-option>
                                        <el-option label="男" value="1"></el-option>
                                        <el-option label="女" value="0"></el-option>
                                    </el-select>
                                </el-col>
                            </el-row>
                            <br/>
                            <el-row>
                                <el-col :span="10" :offset="1" style="margin-left:34px">
                                  <dh style="color:red">*</dh>运动时间:<el-input v-model="form.movementTime" size="mini"/>
                                  </el-col>
                                <el-col :span="10" :offset="1" style="margin-left:44px">
                                  <dh style="color:red">*</dh>运动强度:<el-select v-model="form.strength" size="mini" style="width: 160px;">
                                        <el-option label="低" value="低"></el-option>
                                        <el-option label="中" value="中"></el-option>
                                        <el-option label="高" value="高"></el-option>
                                    </el-select>
                                  </el-col>
                            </el-row>
                            <br/>
                            <el-row>
                                <el-col :span="10" :offset="1" style="margin-left:20px">
                                  <dh style="color:red">*</dh>千步当量:<el-input v-model="form.thousandStepEquivalent"  size="mini"/>
                                  </el-col>
                                <el-col :span="10" :offset="1" style="margin-left:60px">
                                  <dh style="color:red">*</dh>拉伸部位:<el-select v-model="form.tensileParts" size="mini" style="width: 160px;">
                                        <el-option label="上肢" value="上肢"></el-option>
                                        <el-option label="下肢" value="下肢"></el-option>
                                        <el-option label="全身" value="全身"></el-option>
                                    </el-select>
                                  </el-col>
                            </el-row>
                            <br/>
                            <el-row>
                                <el-col :span="10" :offset="1" style="margin-left:34px">
                                  <dh style="color:red">*</dh>年龄下限:<el-input @blur="xx()" v-model="form.minage"  size="mini"/>
                                  </el-col>
                               <el-col :span="10" :offset="1" style="margin-left:38px">
                                 <dh style="color:red">*</dh>年龄上限:<el-input @blur="sx()" v-model="form.maxage"  size="mini"/>
                                 </el-col>
                            </el-row>
                            <br/>
                            <br/>
                            <el-row style="margin-left:15px">
                                <el-col :span="2" :offset="2">动作方法:</el-col>
                                <el-col :span="14"><el-input v-model="form.actionMethod" type="textarea"  :rows="3" size="mini"/></el-col>
                            </el-row>
                            <el-row style="margin-left:15px">
                                <el-col :span="2" :offset="2">注意事项:</el-col>
                                <el-col :span="14"><el-input v-model="form.notes" type="textarea"  :rows="3" size="mini"/></el-col>
                            </el-row>
                        </el-form>
                                <el-row style="margin-top:20px">
                                    <el-col :span="2" :offset="7"><el-button size="mini" type="primary" @click="updStu()" :disabled="bj1">确 定</el-button></el-col>
                                    <el-col :span="2" :offset="7"><el-button size="mini" type="info" @click="dialogFormVisible = false">取 消</el-button></el-col>
                                </el-row>
        </el-dialog>
  </div>
</template>

<script>
import Vue from "vue";
import axios from "axios";
export default Vue.extend({
  data() {
    return {
      loading:true,
      AllUser: [],
      parameter: {
        page: 1,
        num: 3,
        content: "",
      },
      total: 0,
       bj:'',
      bj1:false,
      bj2:false,
      dialogFormVisible : false,
      form:{
        id:'',
        code:'',
        name:'',
        strength:'',
        applyToGender:'',
        thousandStepEquivalent:'',
        movementTime:'',
        minage:'',
        maxage:'',
        actionMethod:'',
        notes:'',
        createTime:'',
        updateTime:'',
      },
    };
  },
  methods: {
      xx() {

		var regs = /^\d{2,}$/;
		if(regs.test(this.form.minage)) {
     
			return true;
		} else {
       this.$message('只能输入数字');
			return false;
		}
	},
    sx() {

		var regs = /^\d{2,}$/;
		if(regs.test(this.form.maxage)) {
     
			return true;
		} else {
       this.$message('只能输入数字');
			return false;
		}
	},
    deleteRow(index,row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           axios.get("/api/lqHealth/delTSport?id="+row.id).then(data=>{
            if(data.data==1){
            this.load();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
              }else{
                 this.$message({
                  type: 'info',
                  message: '此检查项已被套餐选择，不能删除!!'
                 }); 
               }
           });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    handleSelectionChange() {},
   handleSizeChange(val) {
      this.parameter.num=val;
      this.load();
    },
   handleCurrentChange(val) {
      this.parameter.page=val;
      this.load();
    },
    onSubmit() {
      this.load();
    },
    addTSportsItem() {
      this.form={};
      this.dialogFormVisible = true;
      this.bj=true;
      this.bj2=false;
    },
    update(index,row){
      this.bj1=false;
      this.bj2=true;
      this.form=row;
      this.form.applyToGender=row.applyToGender.toString()
      this.bj=false;
      this.dialogFormVisible=true;
    },
    updStu(){
      let upd = new Date();
      if(this.form.minage>this.form.maxage){
         this.$message({
           type: 'info',
           message: '年龄下限必须小于年龄上限，请选择输入！！'
            });
      }else{
      if(this.bj){
        this.form.createTime=upd;
        axios.get("/api/lqHealth/addTSport?tsport="+JSON.stringify(this.form)).then((data) => {
            if(data.data==1){
              this.$message({
                message: '恭喜你，添加成功！！！',
                type: 'success'
                });
              this.load();
            }
        });
      }else{
        this.form.updateTime=upd;
        axios.get("/api/lqHealth/updTSport?tsport="+JSON.stringify(this.form)).then((data) => {
            if(data.data==1){
              this.$message({
                message: '恭喜你，修改成功！！！',
                type: 'success'
                });
              this.load();
            }
        });
      }
    }
      this.dialogFormVisible = false
    },
    //分页加载
    load() {
      axios
        .post("/api/lqHealth/selectAllTSportsItem", this.parameter)
        .then((res) => {
          this.AllUser = res.data.list;
          this.parameter.page = res.data.pageNum;
          this.parameter.num = res.data.pageSize;
          this.total = res.data.total;
          for (let i = 0; i < this.AllUser.length; i++) {
            let count = "";
            count = this.AllUser[i].minage + "—" + this.AllUser[i].maxage;
            this.$set(this.AllUser[i], "count", count);
          }
          
          //加载样式
        setTimeout(() => {
          this.loading=false;
        }, 1000);
        });
    },
  },
  created() {
    this.load();
  },
});
</script>
<style scoped>
.el-input{
  width: 130px;
}
</style>